package com.example.springboot.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class MyExceptionHandler{
    /**
     * 捕获全局异常
     * @param e
     * @return
     */
    @ExceptionHandler({Exception.class}) // 声明捕获哪个异常类
    public String GlobalException(Exception e){
        log.error(e.getMessage(), e);
        return "自定义捕获全局异常";
    }
    /**
     * 捕获空指针异常
     * @param e
     * @return
     */
    @ExceptionHandler({NullPointerException.class})
    public String NullPointerException(Exception e){
        log.error(e.getMessage(), e);
        return "自定义捕获空指针异常";
    }
}